package cn.anquing.service.impl;

import cn.anquing.datasource.DataSource;
import cn.anquing.datasource.DynamicDataSourceHolder;
import cn.anquing.mapper.CityMapper;
import cn.anquing.model.City;
import cn.anquing.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityMapper cityMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return cityMapper.deleteByPrimaryKey(id);
    }

    @Override
//    @DataSource("dataSource2")
    public int insert(City record) {
        return cityMapper.insert(record);
    }

    @Override
    public int insertSelective(City record) {
        return cityMapper.insertSelective(record);
    }

    @Override
    public City selectByPrimaryKey(Long id) {
        // 没有指定，则默认使用数据源1
        return cityMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(City record) {
        return cityMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(City record) {
        return cityMapper.updateByPrimaryKey(record);
    }

    @Override
//    @DataSource("dataSource2")
    public City selectByPrimaryKey2(Long id) {
        // 指定切换到数据源2
//        DynamicDataSourceHolder.setDataSource("dataSource2");
        return cityMapper.selectByPrimaryKey2(id);
    }

    @Override
    public void transitionCity(Long id) {
        City city = selectByPrimaryKey(id);
        insert(city);
    }
}
